import { WS_URL } from "../config";
export const useWebsocket = (handleMessage) => {
    const token=localStorage.getItem('Authorization');
    const ws = new WebSocket(WS_URL+'/?token='+token);

    const init=()=>{
        bindingEvent();
    }

    function bindingEvent(){
        ws.onopen=handleOpen;
        ws.onclose=handleClose;
        ws.onerror=handleError;
        ws.onmessage=handleMessage;
    }

    function handleOpen(e){
        console.log('ws open',e)
    }

    function handleClose(e){
        console.log('ws close',e)
    }

    function handleError(e){
        console.log('ws error',e)
    }

    init();
    return ws;
}